package cc.mrbird.card.service.impl;

import cc.mrbird.card.dao.CardInfoMapper;
import cc.mrbird.card.dao.RefundCardMapper;
import cc.mrbird.card.domain.CardInfo;
import cc.mrbird.card.domain.RefundCard;
import cc.mrbird.card.domain.RefundCardVo;
import cc.mrbird.card.service.CardInfoService;
import cc.mrbird.common.domain.QueryRequest;
import cc.mrbird.common.util.DateUtil;
import cc.mrbird.system.domain.User;
import com.github.pagehelper.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class CardInfoServiceImpl implements CardInfoService {

    @Autowired
    private CardInfoMapper cardInfoMapper;
    @Autowired
    private RefundCardMapper refundCardMapper;

    private Logger log = LoggerFactory.getLogger(this.getClass());


    @Override
    public List<CardInfo> getAllList(CardInfo cardInfo, QueryRequest queryRequest) {
        List<CardInfo> list = new ArrayList<>();
            try{
            list = cardInfoMapper.getAllList(cardInfo);
            return list;
        }catch (Exception ex){
            ex.printStackTrace();
            return list;
        }
    }

    @Override
    @Transactional
    public RefundCard commitRefundCardInfo(CardInfo cardInfo, User user,Double refundamt,Double deposit) throws Exception {
        List<CardInfo> list = getAllList(cardInfo,null);
        if(list == null || list.size() == 0){
            throw new Exception("查询不到卡数据");
        }
        CardInfo cardDBDate = list.get(0);
        if(cardDBDate == null){
            throw new Exception("查询不到卡数据");
        }
        Date nowDate = new Date();
        String now = DateUtil.getDateFormat(nowDate,"yyyyMMddHHmmss");
        RefundCard refundCardWhere = new RefundCard();
        refundCardWhere.setCardno(cardInfo.getCardNo());
        List<RefundCard> refundCardList = refundCardMapper.select(refundCardWhere);
        String orderNum = now+cardDBDate.getCardNo().substring(14,16);
        RefundCard refundCard = new RefundCard();
        if(refundCardList == null || refundCardList.size() == 0){
            /*refundCard.setRefundordernum(orderNum);
            refundCard.setCardno(cardDBDate.getCardNo());
            refundCard.setCardusername(cardDBDate.getCardUserName());
            refundCard.setPaperid(cardDBDate.getPaperId());
            refundCard.setSyscardbanlance(cardDBDate.getCardBanlance().longValue()*100);
            refundCard.setCardtypename(cardDBDate.getCardTypeName());
            refundCard.setCardtype(cardDBDate.getCardType());
            refundCard.setEqpid(null);
            refundCard.setTranstack(null);
            refundCard.setDeposit(deposit.longValue()*100);
            refundCard.setRefundamt(refundamt.longValue()*100);
            refundCard.setCardbanlance(cardInfo.getCardBanlance().longValue()*100);
            refundCard.setRefundusername(user.getUsername());
            refundCard.setRefunduser(user.getUserId().toString());
            refundCard.setRefunddate(nowDate);
            refundCardMapper.insert(refundCard);*/
            refundCard.setRefundordernum(orderNum);
            refundCard.setCardno(cardDBDate.getCardNo());
            refundCard.setCardusername(cardDBDate.getCardUserName());
            refundCard.setPaperid(cardDBDate.getPaperId());
            refundCard.setSyscardbanlance(Long.valueOf(cardDBDate.getAccBanlance().longValue() * 100L));
            refundCard.setCardtypename(cardDBDate.getCardTypeName());
            refundCard.setCardtype(cardDBDate.getCardType());
            refundCard.setEqpid(null);
            refundCard.setTranstack(null);
            deposit = Double.valueOf(deposit.doubleValue() * 100.0D);
            refundCard.setDeposit(Long.valueOf(deposit.longValue()));
            refundamt = Double.valueOf(refundamt.doubleValue() * 100.0D)+deposit;
            refundCard.setRefundamt(Long.valueOf(refundamt.longValue()));
            Double balance = Double.valueOf(cardInfo.getCardBanlance().doubleValue());
            balance = Double.valueOf(balance.doubleValue() * 100.0D);
            refundCard.setCardbanlance(Long.valueOf(balance.longValue()));
            refundCard.setRefundusername(user.getUsername());
            refundCard.setRefunduser(user.getUserId().toString());
            refundCard.setRefunddate(nowDate);
            this.refundCardMapper.insert(refundCard);
        }
        cardInfo.setCardState(11);
        cardInfo.setCardBanlance(new BigDecimal(0));
        cardInfo.setUpdateDateTime(nowDate);
        cardInfoMapper.updateCardState(cardInfo);
        return refundCard;
    }

    public List<RefundCardVo> getRefundCardList(RefundCard refundCard, QueryRequest request)
    {
        List<RefundCardVo> list = new ArrayList();
        try
        {
            List<RefundCard> refundCards = this.cardInfoMapper.getRefundAllList(refundCard);
            for (RefundCard card:refundCards){
                RefundCardVo refundCardVo = new RefundCardVo();
                refundCardVo.setId(card.getId());
                refundCardVo.setRefundordernum(card.getRefundordernum());
                refundCardVo.setCardno(card.getCardno());
                refundCardVo.setCardusername(card.getCardusername());
                refundCardVo.setPaperid(card.getPaperid());
                refundCardVo.setSyscardbanlance(BigDecimal.valueOf(card.getCardbanlance()).divide(new BigDecimal(100),2, RoundingMode.HALF_UP));
                refundCardVo.setCardbanlance(BigDecimal.valueOf(card.getCardbanlance()).divide(new BigDecimal(100),2, RoundingMode.HALF_UP));
                refundCardVo.setDeposit(BigDecimal.valueOf(card.getDeposit()).divide(new BigDecimal(100),2, RoundingMode.HALF_UP));
                refundCardVo.setCardtypename(card.getCardtypename());
                refundCardVo.setCardtype(card.getCardtype());
                refundCardVo.setEqpid(card.getEqpid());
                refundCardVo.setTranstack(card.getTranstack());
                refundCardVo.setRefundamt(BigDecimal.valueOf(card.getRefundamt()).divide(new BigDecimal(100),2, RoundingMode.HALF_UP));
                refundCardVo.setRefunddate(card.getRefunddate());
                refundCardVo.setRefunduser(card.getRefunduser());
                refundCardVo.setRefundusername(card.getRefundusername());
                list.add(refundCardVo);
            }
            return list;
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
        return null;
    }

    /*@Override
    public List<RefundCard> getRefundCardList(RefundCard refundCard, QueryRequest request) {
        List<RefundCard> list = new ArrayList<>();
        try{
            Example example = new Example(RefundCard.class);
            Example.Criteria criteria = example.createCriteria();
            if(StringUtil.isNotEmpty(refundCard.getRefundordernum())){
                criteria.andLike("refundordernum","%" + refundCard.getRefundordernum() + "%");
            }
            if(StringUtil.isNotEmpty(refundCard.getCardno())){
                criteria.andLike("cardno","%" + refundCard.getCardno() + "%");
            }
            if(StringUtil.isNotEmpty(refundCard.getCardusername())){
                criteria.andLike("cardusername","%" + refundCard.getCardusername() + "%");
            }
            if(StringUtil.isNotEmpty(refundCard.getPaperid())){
                criteria.andLike("paperid","%" + refundCard.getPaperid() + "%");
            }
            list = refundCardMapper.selectByExample(example);
            for (RefundCard card:list){
                card.setSyscardbanlance(card.getSyscardbanlance()/100);
                card.setCardbanlance(card.getCardbanlance()/100);
                card.setRefundamt(card.getRefundamt()/100);
                card.setDeposit(card.getDeposit()/100);
            }
            return list;
        }catch (Exception ex){
            ex.printStackTrace();
            return list;
        }
    }*/
}
